package example.jaxws;

import com.franz.ag.AllegroGraphException;
import connection.AGConnecting;
import org.jdom.Document;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Qiang
 * Date: 27.11.2008
 * Time: 16:08:03
 */
public class SearchProcess {

    /*F*/
    public static String creatResult(String searchInput) {
        int resultID = NewIDGener.newId("result");
        String filename = "result_" + resultID + ".xml";
        String filepath = "D:\\glassfish\\domains\\domain1\\docroot\\" + filename;
        // result file will be putted into the docroot on glassfish application server

        Document result = new Document();
        try {
            result = AGConnecting.runSearch(searchInput); // run search process on Allegro Server
        } catch (AllegroGraphException e) {
            e.printStackTrace();
        }

        // XML file format for JDom output
        XMLOutputter outputter = new XMLOutputter();
        Format fmt = Format.getPrettyFormat();
        fmt.setIndent("  ");
        outputter.setFormat(fmt);

        // output the result XML file
        try {
            FileOutputStream fo = new FileOutputStream(filepath);
            outputter.output(result, fo);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "http://localhost:8080/" + filename; // the filepath on server will be return to client
    }
}
